import os
import sys
from datetime import datetime

import allure
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from config.readconfig import Read
#实例化调用
readcon = Read()

@pytest.fixture(scope="session")
def driver():
    browser_type = readcon.get_browser_type()
    if browser_type == "chrome":
        driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
    elif browser_type == "firefox":
        # driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
        driver=webdriver.Firefox()
    else:
        raise ValueError(f"不支持的浏览器类型: {browser_type}")

    driver.maximize_window()
    yield driver
    driver.quit()

#类似pytest公共方法
@pytest.fixture(scope="session")
def test_login(driver):
    driver.get(readcon.get_shchUrl("baseurl"))
    driver.find_element(By.XPATH, '//*[@id="user-name"]').send_keys(readcon.get_user_info("username"))
    driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(readcon.get_user_info("password"))
    driver.find_element(By.XPATH, '//*[@id="login-button"]').click()
    # 显式等待登录成功（验证首页元素）
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CLASS_NAME, "app_logo"))
    )
    return driver



